﻿

$(document).ready(function () {
    var name = getUrlArgStr("name");
    var type = getUrlArgStr("nick");
    name = decodeURI(name);
    type = decodeURI(type);
    var typename = "";
    switch (Trim(type)) {
        case "名称": typename = "nick_name"; break;
        case "手机": typename = "phone"; break;
        case "QQ": typename = "qq"; break;
        case "邮箱": typename = "email"; break;
        case "推广电话": typename = "tel"; break;
    }
    $(".topcententxt").html(type.replace(/(^\s*)|(\s*$)/g, ""));
    $(".nicknametext").val(name.replace(/(^\s*)|(\s*$)/g, ""));
    $("title").html("微街秀 > " + type.replace(/(^\s*)|(\s*$)/g, ""));
    $(".nicksave").click(function () {
        if (!islogin()) { window.location.href = 'login.html'; return; }
  
        if (typename == 'nick_name' || typename == 'email') {
            if ($(".nicknametext").val() == "") {
                Alert("请输入" + type);
                return;
            }
        }
        if (typename == "phone") {
            if (!checkMobile($(".nicknametext").val())) {
                Alert("电话格式不正确");
                $(".nicknametext").focus();
                return;
            }
        }
        else if (typename == "email") {
            if (!/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test($(".nicknametext").val())) {
                Alert("邮箱格式不正确");
                $(".nicknametext").focus();
                return;
            }
        }
        else if (typename == "qq") {
            if (/\D/.test($(".nicknametext").val())) {
                Alert("请输入正确的QQ");
                $(".nicknametext").focus();
                return;
            }
        }

        ajax.UpdateInfo(typename + "=" + $(".nicknametext").val(), function (data) {
            if (data.code == "0")
                window.location.href = "Personal.html";
            else
                Alert(data.msg);
        });
    });

    $(".nickicon").click(function () {
        $("#nicknametext").val("");
        $("#nicknametext").focus();
    });
});

function Trim(str) {
    return str.replace(/(^\s*)|(\s*$)/g, "");
}
/* 
* 功能：判断用户输入的手机号格式是否正确 
* 传参：无 
* 返回值：true or false 
*/
function checkMobile(s) {
    var regu = /^1\d{10}$/;
    var molie = /^0\d{2,3}-?\d{7,8}$/;
    var reg400 = /^400-([0-9]){1}([0-9-]{7})$/;

    var moliereg = new RegExp(molie);
    var re400 = new RegExp(reg400);
    var re = new RegExp(regu);

    if (re400.test(s) || moliereg.test(s) || re.test(s)) {
        return true;
    } else {
        return false;
    }
} 